//
//  SelectTypeView.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/10/16.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit

class SelectTypeView: UIView {

    var didSelectType: (CheckModelType) -> () = {_ in}
    let backgroundView = UIView()
    let contentView = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(handleGes(_:))))
        
        contentView.frame = CGRect(x: 0, y: SCREEN_HEIGHT, width: SCREEN_WIDTH, height: 280 + UIApplication.shared.bottomSafeDistance)
        contentView.backgroundColor = UIColor.white
        self.addSubview(contentView)
        
        let sepator = UIView().config.backgroundColor(#colorLiteral(red: 0.9176470588, green: 0.9176470588, blue: 0.9176470588, alpha: 1))
            .addTo(contentView)
            .makeConstraints { (make) in
                make.left.right.equalToSuperview()
                make.height.equalTo(1)
                make.top.equalTo(50)
        }.view
        
        let titleLabel = UILabel().config
            .text("快速获取序列号".localized)
            .textColor(#colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1))
            .font(UIFont.systemFont(ofSize: 16, weight: .medium))
            .addTo(contentView)
            .makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.centerY.equalTo(contentView.snp.top).offset(25)
        }.view
        
        UIButton().config
            .image(UIImage.init(named: "close"))
            .addTarget(self, action: #selector(dismissAnimation))
            .addTo(contentView)
            .makeConstraints { (make) in
                make.left.equalTo(10)
                make.height.width.equalTo(40)
                make.centerY.equalTo(titleLabel.snp.centerY)
        }
        
        let NFCView = SelectTypeItemView()
        NFCView.setType(.NFC)
        NFCView.selectType = { [weak self] in
            self?.handleNFC()
        }
        contentView.addSubview(NFCView)
        NFCView.snp.makeConstraints { (make) in
            make.top.equalTo(sepator.snp.bottom).offset(40)
            make.left.equalTo(40)
            make.centerX.equalToSuperview()
            make.height.equalTo(60)
        }
        
        let scanView = SelectTypeItemView()
        scanView.setType(.scan)
        scanView.selectType = { [weak self] in
            self?.handleScan()
        }
        contentView.addSubview(scanView)
        scanView.snp.makeConstraints { (make) in
            make.top.equalTo(NFCView.snp.bottom).offset(30)
            make.left.equalTo(40)
            make.centerX.equalToSuperview()
            make.height.equalTo(60)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func handleNFC() {
        didSelectType(.NFC)
        dismissAnimation()
    }
    
    func handleScan() {
        didSelectType(.scan)
        dismissAnimation()
    }
    
    @objc func handleGes(_ ges: UIGestureRecognizer) {
        let location = ges.location(in: self)
        if contentView.frame.contains(location) { return }
        dismissAnimation()
    }
    
    func showAnimation() {
        self.frame = UIScreen.main.bounds
        UIApplication.shared.keyWindow?.addSubview(self)
        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 5, options: .curveEaseInOut, animations: {
            self.backgroundColor = UIColor.init(white: 0, alpha: 0.3)
            self.contentView.y = SCREEN_HEIGHT - self.contentView.height
        })
    }
    
    @objc func dismissAnimation() {
        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 5, options: .curveEaseInOut, animations: {
            self.backgroundColor = .clear
            self.contentView.y = SCREEN_HEIGHT
        }) { (_) in
            self.removeFromSuperview()
        }
    }
    
}

class SelectTypeItemView: UIView {
    
    let imageView = UIImageView()
    let titleLabel = UILabel()
    var selectType: () -> () = {}
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = #colorLiteral(red: 0.9927800298, green: 0.9693011642, blue: 0.9273402095, alpha: 1)
        
        self.layer.masksToBounds = true
        self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(didSelect)))
        
        imageView.config
            .contentMode(.scaleAspectFit)
            .addTo(self)
            .makeConstraints { (make) in
                make.left.equalTo(20)
                make.width.equalTo(30)
                make.centerY.equalToSuperview()
                make.height.equalTo(50)
        }
        
        titleLabel.config
            .textColor(APPConfig.baseColor)
            .font(.systemFont(ofSize: 19, weight: .medium))
            .addTo(self)
            .makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.left.equalTo(imageView.snp.right).offset(15)
        }
        
        let arrowImage = UIImageView()
        arrowImage.contentMode = .scaleAspectFit
        arrowImage.image = UIImage.init(named: "right_arrow")
        self.addSubview(arrowImage)
        arrowImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-15)
            make.height.width.equalTo(18)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func didSelect() {
        self.selectType()
    }
    
    func setType(_ type: CheckModelType) {
        imageView.image = type.image
        titleLabel.text = type.selectTitle
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.layer.cornerRadius = self.height * 0.5
    }
}

extension CheckModelType {
    
    var selectTitle: String {
        switch self {
        case .NFC:
            return "识别芯片标签获取".localized
        case .scan:
            return "扫描条形码获取".localized
        }
    }
    
}
